package info.wangyuan.agent.controller.admin;


import info.wangyuan.agent.entity.po.QrCode;
import info.wangyuan.agent.service.curd.QrCodeService;
import info.wangyuan.agent.utils.Result;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Albert
 * @since 2025-08-04 12:55:59
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/qrcode")
public class QrCodeController {

    private final QrCodeService qrCodeService;

    public QrCodeController(QrCodeService qrCodeService) {
        this.qrCodeService = qrCodeService;
    }

    @PostMapping("/upload")
    public Result<Void> uploadQrCode(@RequestParam MultipartFile file,
                                     @RequestParam @Valid String name) {
        qrCodeService.saveQrCode(file, name);
        return Result.success();
    }

    @GetMapping("/get/{id}")
    public String getQrCode(@PathVariable @NotNull Integer id) {
        return qrCodeService.getQrCodeBase64(id);
    }

    @GetMapping(value = "/page/{id}", produces = MediaType.TEXT_HTML_VALUE)
    public String renderHtml(@PathVariable Integer id) {
        QrCode qrCode = qrCodeService.getById(id);
        String name = qrCode.getName();
        String base64 = "data:image/png;base64," + qrCode.getBase64();
        return """
                <!DOCTYPE html>
                <html lang="zh">
                <head>
                    <meta charset="UTF-8">
                    <meta name="viewport"
                          content="width=device-width, initial-scale=1.0">
                    <title>%s二维码</title>
                    <style>
                        body {
                            margin: 0;
                            padding: 0;
                            font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
                            background-color: #f4f6f8;
                            height: 100vh;
                            display: flex;
                            flex-direction: column;
                            align-items: center;
                            justify-content: center;
                        }
                        h1 {
                            color: #333;
                            margin-bottom: clamp(0, 1.5vw + 1.5vh, 30px);
                            /* clamp(最小值, 理想值, 最大值) */
                            font-size: clamp(
                                10px,                 /* 最小字体 */
                                2vw + 2vh,            /* 动态计算：同时受宽度(vw)和高度(vh)影响 */
                                20px                  /* 最大字体 */
                            );
                            transition: font-size 0.2s ease, margin-bottom 0.2s ease;
                        }
                        .qr-container {
                            background-color: #fff;
                            padding: 20px 30px;
                            border-radius: 12px;
                            box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
                            text-align: center;
                            max-height: 80vh;
                            transition: max-height 0.2s ease;
                        }
                        img {
                            max-width: 400px;
                            max-height: 60vh;
                            transition: max-width 0.2s ease, max-height 0.2s ease;
                        }
                        /* 手机屏幕自适应 */
                        @media (max-width: 600px) {
                            h1 {
                                font-size: 16px;
                            }
                            img {
                                max-width: 250px;
                            }
                        }
                        /* 特别小的屏幕（如小屏手机） */
                        @media (max-width: 400px) {
                            h1 {
                                font-size: 10px;
                            }
                            img {
                                max-width: 200px;
                            }
                        }
                        /* 针对 Chrome / Safari / Edge */
                        ::-webkit-scrollbar {
                            display: none; /* 隐藏滚动条 */
                        }
                        /* 针对 Firefox */
                        * {
                            scrollbar-width: none; /* 隐藏滚动条 */
                        }
                        /* 针对 IE、旧 Edge */
                        * {
                            -ms-overflow-style: none;
                        }
                    </style>
                </head>
                <body>
                <div class="qr-container">
                    <h1>%s二维码</h1>
                    <img id="qrImg" src="%s" alt="二维码加载中..." />
                </div>
                </body>
                </html>
                """.formatted(name, name, base64);
    }

    @PostMapping("/change")
    public Result<Void> changeQrCode(@RequestParam MultipartFile file,
                                     @RequestParam @NotNull Integer id) {
        qrCodeService.updateQrCode(file, id);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result<Void> deleteQrCode(@PathVariable @NotNull Integer id) {
        qrCodeService.removeById(id);
        return Result.success();
    }
}
